home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 419_01 / odmg10 / util / scripts / mdepend.cpp < prev    next >
Encoding:
Text File  |  1993-09-09  |  3.7 KB  |  229 lines

  1. /**/#!/bin/sh
  2. /**/#
  3. /**/#    @(#)mdepend.sh    1.3    5/11/88 19:44:36
  4. /**/#
  5. /**/#    Do the equivalent of the 'makedepend' program, but do it right.
  6. /**/#
  7. /**/#    Usage:
  8. /**/#
  9. /**/#    makedepend [cpp-flags] [-w width] [-s magic-string] [-f makefile]
  10. /**/#      [-o object-suffix]
  11. /**/#
  12. /**/#    Notes:
  13. /**/#
  14. /**/#    The C compiler used can be overridden with the environment
  15. /**/#    variable "CC".
  16. /**/#
  17. /**/#    The "-v" switch of the "makedepend" program is not supported.
  18. /**/#
  19. /**/#
  20. /**/#    This script should
  21. /**/#    work on both USG and BSD systems.  However, when System V.4 comes out,
  22. /**/#    USG users will probably have to change "silent" to "-s" instead of
  23. /**/#    "-" (at least, that's what the documentation implies).
  24. /**/#
  25. CC=PREPROC
  26.  
  27. silent='-'
  28.  
  29. TMP=/tmp/mdep$$
  30. CPPCMD=${TMP}a
  31. DEPENDLINES=${TMP}b
  32. TMPMAKEFILE=${TMP}c
  33. MAGICLINE=${TMP}d
  34. ARGS=${TMP}e
  35.  
  36. trap "rm -f ${TMP}*; exit 1" 1 2 15
  37. trap "rm -f ${TMP}*; exit 0" 1 2 13
  38.  
  39. echo " \c" > $CPPCMD
  40. if [ `wc -c < $CPPCMD` -eq 1 ]
  41. then
  42.     c="\c"
  43.     n=
  44. else
  45.     c=
  46.     n="-n"
  47. fi
  48.  
  49. echo $n "$c" >$ARGS
  50.  
  51. files=
  52. makefile=
  53. magic_string='# DO NOT DELETE'
  54. objsuffix='.o'
  55. width=78
  56. endmarker=""
  57. verbose=n
  58.  
  59. while [ $# != 0 ]
  60. do
  61.     if [ "$endmarker"x != x -a "$endmarker" = "$1" ]; then
  62.     endmarker=""
  63.     else
  64.     case "$1" in
  65.         -D*|-I*)
  66.         echo $n " '$1'$c" >> $ARGS
  67.         ;;
  68.  
  69.         -g|-o)
  70.         ;;
  71.  
  72.         *)
  73.         if [ "$endmarker"x = x ]; then
  74.             case "$1" in     
  75.             -w)
  76.                 width="$2"
  77.                 shift
  78.                 ;;
  79.             -s)
  80.                 magic_string="$2"
  81.                 shift
  82.                 ;;
  83.             -f)
  84.                 makefile="$2"
  85.                 shift
  86.                 ;;
  87.             -o)
  88.                 objsuffix="$2"
  89.                 shift
  90.                 ;;
  91.             
  92.             --*)
  93.                 echo "$1" | sed 's/^\-\-//' >${TMP}end
  94.                 endmarker="`cat ${TMP}end`"
  95.                 rm -f ${TMP}end
  96.                 if [ "$endmarker"x = x ]; then
  97.                 endmarker="--"
  98.                 fi
  99.                 ;;
  100.             -v)
  101.                 verbose="y"
  102.                 ;;
  103.  
  104.             -cc)
  105.                 CC="$2"
  106.                 shift
  107.                 ;;
  108.  
  109.             -*)
  110.                 echo "Unknown option '$1' ignored" 1>&2
  111.                 ;;
  112.             *)
  113.                 files="$files $1"
  114.                 ;;
  115.             esac
  116.         fi
  117.         ;;
  118.     esac
  119.     fi
  120.     shift
  121. done
  122. echo ' $*' >> $ARGS
  123.  
  124. echo "exec $CC `cat $ARGS`" > $CPPCMD
  125. chmod +x $CPPCMD
  126. rm $ARGS
  127.  
  128. case "$makefile" in
  129.     '')
  130.     if [ -r makefile ]
  131.     then
  132.         makefile=makefile
  133.     elif [ -r Makefile ]
  134.     then
  135.         makefile=Makefile
  136.     else
  137.         echo 'no makefile or Makefile found' 1>&2
  138.         exit 1
  139.     fi
  140.     ;;
  141.     -)
  142.     makefile=$TMPMAKEFILE
  143.     ;;
  144. esac
  145.  
  146. if [ "$verbose"x = "y"x ]; then 
  147.     cat $CPPCMD
  148. fi
  149.  
  150. echo '' > $DEPENDLINES
  151. for i in $files
  152. do
  153.     $CPPCMD $i \
  154.       | sed -n "/^#/s;^;$i ;p"
  155. done \
  156.   | sed -e 's|/[^/.][^/]*/\.\.||g' -e 's|/\.[^.][^/]*/\.\.||g' \
  157.     -e 's|"||g' -e 's| \./| |' \
  158.   | awk '{
  159.     if ($1 != $4  &&  $2 != "#ident")
  160.         {
  161.         ofile = substr ($1, 1, length ($1) - 2) "'"$objsuffix"'"
  162.         print ofile, $4
  163.         }
  164.     }' \
  165.   | sort -u \
  166.   | awk '
  167.         {
  168.         newrec = rec " " $2
  169.         if ($1 != old1)
  170.         {
  171.         old1 = $1
  172.         if (rec != "")
  173.             print rec
  174.         rec = $1 ": " $2
  175.         }
  176.         else if (length (newrec) > '"$width"')
  177.         {
  178.         print rec
  179.         rec = $1 ": " $2
  180.         }
  181.         else
  182.         rec = newrec
  183.         }
  184.     END \
  185.         {
  186.         if (rec != "")
  187.         print rec
  188.         }' \
  189.   | egrep -v '^[^:]*:[     ]*$' >> $DEPENDLINES
  190.  
  191. trap "" 1 2 13 15    # Now we are committed
  192. case "$makefile" in
  193.     $TMPMAKEFILE)
  194.     ;;
  195.     *)
  196.     rm -f Makefile.bak
  197.     cp $makefile $makefile.bak
  198.     echo "Appending dependencies to $makefile"
  199.     ;;
  200. esac
  201.  
  202. /**/#
  203. /**/# Append the magic string and a blank line so that /^$magic_string/+1,\$d
  204. /**/# can be used to delete everything from after the magic string to the end
  205. /**/# of the file.  Then, append a blank line again and then the dependencies.
  206. /**/#
  207. cat >> $makefile <<- END_OF_APPEND
  208.  
  209.     $magic_string
  210.  
  211. END_OF_APPEND
  212. ed $silent $makefile <<- END_OF_ED_SCRIPT
  213. /^$magic_string/+1,\$d
  214. w
  215. q
  216. END_OF_ED_SCRIPT
  217. echo '' >>$makefile
  218. cat $DEPENDLINES >>$makefile
  219.  
  220. case "$makefile" in
  221.     $TMPMAKEFILE)
  222.     cat $TMPMAKEFILE
  223.     ;;
  224.  
  225. esac
  226.  
  227. rm -f ${TMP}*
  228. exit 0
  229.